package com.wwwc.index.web.servlet;

import java.io.*;
import java.util.Properties;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException; 

// by extends DefaultHandler, you do not need list all methods
public class ConfigParser extends DefaultHandler {

    private Properties props;
    private String currentSet;
    private String currentName;
    private StringBuffer currentValue = new StringBuffer();

    //init
    public ConfigParser() {
	this.props = new Properties();
    }

    public Properties getProps() {
	return this.props;
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) {
	try {
	    currentValue.delete(0, currentValue.length());
	    this.currentName = qName;
	}
	catch (Exception e){
	    System.out.println("ConfigParser:Error:["+e+"]");
	}
    }

    public void characters(char[] ch, int start, int length) {
	try {
	    currentValue.append(ch, start, length);
	}
	catch(Exception e){ 
	    System.out.println("ConfigParser:Error:["+e+"]");
	}
    }

    public void endElement(String uri, String localName, String qName) {
	try {
	    props.put(qName.toLowerCase(), currentValue.toString().trim());
	}
	catch(Exception e){ 
	    System.out.println("ConfigParser:Error:["+e+"]");
	}
    }
}





